<!DOCTYPE html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script>
      run(async function () {
        const clock = lolex.createClock(new Date('2022-11-11T19:34:00.000Z'));

        const { directLine, store } = testHelpers.createDirectLineEmulator({ ponyfill: clock });

        WebChat.renderWebChat(
          { directLine, ponyfill: clock, store, userID: 'u-00001' },
          document.getElementById('webchat')
        );

        await pageConditions.webChatRendered();

        clock.tick(1000);

        // Related to issue #4501.
        // WHEN: An outgoing activity arrived via chat history restoration.
        await directLine.emulateIncomingActivity({
          type: 'message',
          id: 'c00001-us|0000000',
          timestamp: '2022-11-11T19:34:00.000Z',
          // All messages sent from end-user must have `localTimestamp`.
          // If `localTimestamp` is not present, this will not repro.
          localTimestamp: '2022-11-11T11:34:00.000-08:00',
          from: {
            id: 'u00001',
            role: 'user'
          },
          text: 'Hello, World!',
          channelData: {
            clientActivityID: 'ca00001'
          }
        });

        // THEN: Activity status on the outgoing activity should be "sent".
        //       This is because all activities restored from service, are successfully sent.
        expect(
          pageElements.activityStatuses().map(element => element.querySelector('[aria-hidden="true"]').innerText)
        ).toEqual(['Just now']);
      });
    </script>
  </body>
</html>
